<!--
 * @Description: 
 * @Author: charles
 * @Date: 2021-11-11 11:19:29
 * @LastEditors: charles
 * @LastEditTime: 2021-11-11 11:38:04
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>高阶组件-反向继承</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/react/17.0.2/umd/react.development.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/react-dom/17.0.2/umd/react-dom.development.min.js"></script>
  <script src="https://cdn.bootcss.com/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
  <div id="app"></div>
  <script type="text/babel">

    class List extends React.Component{
      render(){
        return (
          <div>
            <h3>列表</h3>  
            <div>{this.props.num}</div>
            <div>state:{JSON.stringify(this.state)}</div>
          </div>
        )
      }
    }
    // 高阶组件 - 将一些共性特征（属性，方法）状态注入到指定组件
    function withUser(Component){
      return class H extends Component{
        constructor(props){
          super(props);
          this.state = {
            user:{
              name:"terry",
              age:12
            }
          }
        }
        render(){
          return super.render();
        }
      }
    }
    List = withUser(List);
  
    class App extends React.Component {
      render(){
        return (
          <div>
            <h2>组件通信</h2>  
            <List num={1}></List>
          </div>
        )
      }
    }
    ReactDOM.render(<App/>,document.getElementById('app'));
  </script>
</body>
</html>